use crate::backend::CodeGraphBackend;
use tower_lsp::{LspService, Server};
use tokio::io::{stdin, stdout};

/// Start the LSP server
pub async fn start_lsp_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    tracing::info!("Starting CodeGraph-RT LSP Server");

    let stdin = stdin();
    let stdout = stdout();

    let (service, socket) = LspService::new(|client| CodeGraphBackend::new(client));
    
    Server::new(stdin, stdout, socket)
        .serve(service)
        .await;

    Ok(())
}

/// Configuration for the LSP server
#[derive(Debug, Clone)]
pub struct LspConfig {
    /// Enable real-time diagnostics
    pub enable_diagnostics: bool,
    /// Enable code completion
    pub enable_completion: bool,
    /// Enable hover information
    pub enable_hover: bool,
    /// Enable go-to-definition
    pub enable_definition: bool,
    /// Enable find references
    pub enable_references: bool,
    /// Enable document symbols
    pub enable_symbols: bool,
    /// Enable code formatting
    pub enable_formatting: bool,
    /// Enable code actions and refactoring
    pub enable_code_actions: bool,
    /// Maximum number of completion items
    pub max_completion_items: usize,
    /// Diagnostic update delay in milliseconds
    pub diagnostic_delay_ms: u64,
    /// Enable incremental text synchronization
    pub incremental_sync: bool,
}

impl Default for LspConfig {
    fn default() -> Self {
        Self {
            enable_diagnostics: true,
            enable_completion: true,
            enable_hover: true,
            enable_definition: true,
            enable_references: true,
            enable_symbols: true,
            enable_formatting: true,
            enable_code_actions: true,
            max_completion_items: 100,
            diagnostic_delay_ms: 500,
            incremental_sync: true,
        }
    }
}
